import api from '@/api';

export default {
  namespace: 'memberReport',

  state: {
    cashReports: {
      list: [],
      pagination: {},
    },
    cashTotalReports: {
      list: [],
      pagination: {},
    },
    securityReports: {
      list: [],
      pagination: {},
    },
    securityTotalReports: {
      list: [],
      pagination: {},
    },
    beanReports: {
      list: [],
      pagination: {},
    },
    beanTotalReports: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetchCash({ payload }, { call, put }) {
      const response = yield call(api.memberReport.queryCash, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCashReports',
          payload: response,
        });
      }
      return response;
    },
    *fetchCashTotal({ payload }, { call, put }) {
      const response = yield call(api.memberReport.queryCashTotal, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCashTotalReports',
          payload: response,
        });
      }
      return response;
    },
    *fetchSecurity({ payload }, { call, put }) {
      const response = yield call(api.memberReport.querySecurity, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveSecurityReports',
          payload: response,
        });
      }
      return response;
    },

    *fetchSecurityTotal({ payload }, { call, put }) {
      const response = yield call(api.memberReport.querySecurityTotal, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveSecurityTotalReports',
          payload: response,
        });
      }
      return response;
    },
    *fetchBean({ payload }, { call, put }) {
      const response = yield call(api.memberReport.queryBean, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveBeanReports',
          payload: response,
        });
      }
      return response;
    },
    *fetchBeanTotal({ payload }, { call, put }) {
      const response = yield call(api.memberReport.queryBeanTotal, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveBeanTotalReports',
          payload: response,
        });
      }
      return response;
    },
  },

  reducers: {
    saveCashReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        cashReports: {
          list: data,
          pagination,
        },
      };
    },
    clearCashReports() {
      return {
        cashReports: {
          list: [],
          pagination: {},
        },
      };
    },
    saveCashTotalReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        cashTotalReports: {
          list: data,
          pagination,
        },
      };
    },
    clearCashTotalReports() {
      return {
        cashTotalReports: {
          list: [],
          pagination: {},
        },
      };
    },
    saveSecurityReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        securityReports: {
          list: data,
          pagination,
        },
      };
    },
    clearSecurityReports() {
      return {
        securityReports: {
          list: [],
          pagination: {},
        },
      };
    },
    saveSecurityTotalReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        securityTotalReports: {
          list: data,
          pagination,
        },
      };
    },
    clearSecurityTotalReports() {
      return {
        securityTotalReports: {
          list: [],
          pagination: {},
        },
      };
    },
    saveBeanReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        beanReports: {
          list: data,
          pagination,
        },
      };
    },
    clearBeanReports() {
      return {
        beanReports: {
          list: [],
          pagination: {},
        },
      };
    },
    saveBeanTotalReports(state, action) {
      const { data, pagination } = action.payload;
      return {
        ...state,
        beanTotalReports: {
          list: data,
          pagination,
        },
      };
    },
    clearBeanTotalReports() {
      return {
        beanTotalReports: {
          list: [],
          pagination: {},
        },
      };
    },
  },
};
